﻿/**
 * ...
 * @author Артём Григорьев
 */

package tab;

import flash.Lib;
import flash.text.TextFormat;
import gui.hug.HUGLinkButton;
import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.HUGTextArea;
import gui.hug.Tab;
import list.ImageLoader;

import list.SearchPanel;
import list.AddItems;
import list.NewList;
import list.OpenTabEvent;

import loader.Loader;

import client.Item;

class ItemsTab extends Tab
{
	private var lengthNewlist   : UInt;
	private var channelId   	: UInt;
	private var channelName 	: String;
	private var loader 			: Loader;
	private var sprite 			: HUGSprite;
	private var newsList 		: NewList;
	private var search   		: SearchPanel;
	private var addItems		: AddItems;
	private var limitItems  	: UInt ;
	private var loading         : HUGTextArea;
	
	public function new(channelId : UInt, channelName : String, panel : HUGTabbedPanel = null, uid : Int) 
	{
		// initialization
		var me = this;
		sprite = new HUGSprite();
		Loader.createInstance();
		loader = Loader.getInstance();
		loading = new HUGTextArea();
		
		//loadingImage;
		
		loading.setLocation(220, 170);
		loading.setSize(200, 50);
		loading.text = "Загрузка...";
		loading.setTextFormat(new TextFormat ("Verdana", 30, 0x000080)) ;
		loading.border = false;
	
		
		me.channelId 	= channelId;
		me.channelName  = channelName;
		limitItems = 10;
		
		search 		= new SearchPanel(187, 0,function (text: String) { me.hideItems(text);});							
		addItems 	= new AddItems(187, 5 , function () { me.addload();});
		newsList 	= new NewList(0, Std.int(search.height) + 20, 600);
		
		// creating layout
		sprite.addChild(search);
		sprite.addChild(newsList);
		sprite.addChild(loading);
		sprite.addChild(addItems);
		addItems.visible = false;
		
		// loading data from server
		loader.getItems(onLoad, me.channelId, limitItems);
		
 
		super(sprite, this.channelName, 1, panel, true);
	}

	public function onLoad(data : Array < Item > ) : Void {

		for (item in data)
		{
			newsList.addItem(item, 1);
			this.loading.visible = false;
		}
		lengthNewlist = newsList.getQuantity();
		
		if ((lengthNewlist != 0) && ( (lengthNewlist - limitItems) % 10 == 0 )) addItems.visible = true;
		if (lengthNewlist == 0) 
		   {
			   loading.setLocation(190, 170);
			   loading.setSize(400, 100);
			   this.loading.text = "По вашему запросу ничего не найдено!";
			   loading.setTextFormat(new TextFormat ("Verdana", 30, 0x000080)) ;
		   }
		// 0..9 == 10
	}	
	
	public function hideItems(text : String) {
		var i : Int = 0;
		var length = newsList.getQuantity();
		while (i < length) {
			var item = newsList.getItem(i);
			var name = item.getName();
			var descr = item.getDescr();
			
			if ((Std.string(name).toLowerCase().indexOf(text.toLowerCase()) == -1) &&
				(Std.string(descr).toLowerCase().indexOf(text.toLowerCase()) == -1))
			{
			    newsList.hide(i, true);
				newsList.draw();
	
			};
			i++;   
		 }
	}
	public function addload() {
		limitItems += 10;
		loader.getItems(onLoad, channelId, limitItems);
		lengthNewlist = newsList.getQuantity();
		if ((limitItems - lengthNewlist) % 10 != 0) sprite.removeChild(addItems);
		newsList.draw();
		
	}
	
}